<!-- *Author: 黄旗, *Date: 2020-08-04 15:45:59 -->
<template>
    <Modal v-model='modal' :title="edit ? '新增角色' : '编辑角色'" :mask-closable='false' @on-cancel='canceledit(modalname)'>
        <Form :label-width='120' :ref='modalname' :model='modalparams' :rules='createRules'>
            <FormItem label='角色名称：' prop='name'>
                <Input clearable v-model='modalparams.name' placeholder='请填写角色名称' />
            </FormItem>
            <!-- <FormItem label='状态：'>
                <RadioGroup v-model="modalparams.status">
                    <Radio v-for="item of raidolist" :key="item.value" :label="item.value">{{item.label}}</Radio>
                </RadioGroup>
            </FormItem> -->
        </Form>
        <div slot='footer'>
            <Button @click='canceledit(modalname)'>取消</Button>
            <Button type='primary' @click='submitedit(modalname)'>保存</Button>
        </div>
    </Modal>
</template>

<script>
    import {
        addRoleList,
        editRoleList
    } from '@/api/powersetting.js';
    export default {
        name: '',
        components: {},

        data () {
            return {
                modalname: 'modalname',
                modal: false,
                edit: false,
                raidolist: [{ value: 1, label: '启用' }, { value: 0, label: '禁用' }],
                modalparams: { id: 0, name: '', status: 1 },
                createRules: {
                    name: [{
                        required: true,
                        message: '请填写角色名称',
                        trigger: 'blur'
                    }]
                }
            };
        },
        /* 函数方法集合 */
        methods: {
            /* @函数功能: 初始化 */
            initial (row) {
                this.$refs[this.modalname].resetFields();
                this.modalparams = { id: 0, name: '', status: 1 },
                this.askData(row)
            },
            askData (row) {
                this.modal = true
                this.edit = false
                if (row) {
                    this.modalparams = {
                        id: row.id,
                        name: row.name,
                        status: +row.status
                    }
                } else {
                    this.edit = true
                }
            }, // 请求数据处理
            canceledit (name) {
                this.$refs[name].resetFields();
                this.modal = false
            },
            submitedit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        if (this.edit) {
                            addRoleList(this.modalparams).then(() => {
                                this.$Message.success('新增成功')
                                this.$emit('askData')
                                this.modal = false
                            })
                        } else {
                            editRoleList(this.modalparams).then(() => {
                                this.$Message.success('编辑成功')
                                this.$emit('askData')
                                this.modal = false
                            })
                        }
                    }
                })
            }
        }
    }
</script>
